package com.codeest.geeknews.presenter.main;

import com.codeest.geeknews.app.Constants;
import com.codeest.geeknews.base.BaseView;
import com.codeest.geeknews.base.RxPresenter;
import com.codeest.geeknews.base.contract.main.DoubanContract;
import com.codeest.geeknews.model.DataManager;
import com.codeest.geeknews.model.bean.DoubanChannelBean;
import com.codeest.geeknews.model.bean.DoubanLoginBean;
import com.codeest.geeknews.model.bean.DoubanLyricBean;
import com.codeest.geeknews.model.bean.DoubanPlaylistBean;
import com.codeest.geeknews.util.RxUtil;
import com.codeest.geeknews.widget.CommonSubscriber;

import javax.inject.Inject;

import io.reactivex.functions.Function;

/**
 * Created by admin on 2017/8/22.
 */

public class DoubanPresenter extends RxPresenter<BaseView> implements DoubanContract.Presenter {

    private DataManager mDataManager;

    @Inject
    public DoubanPresenter(DataManager mDataManager) {
        this.mDataManager = mDataManager;
    }


    @Override
    public void login(String name, String pwd) {
        if (!(mView instanceof DoubanContract.LoginView)) {
            throw new RuntimeException(mView.getClass().getName() + "must implements " + DoubanContract.LoginView.class.getName());
        }
        addSubscribe(mDataManager.loginDouban(name, pwd)
                .compose(RxUtil.<DoubanLoginBean>rxSchedulerHelper())
                .map(new Function<DoubanLoginBean, DoubanLoginBean>() {
                    @Override
                    public DoubanLoginBean apply(DoubanLoginBean doubanLogin) {
                        //return lastfm.replaceAll("#text", "text").replaceAll("@attr", "attr");
                        mDataManager.insertDoubanAccount(doubanLogin);
                        return doubanLogin;
                    }
                })
                .subscribeWith(new CommonSubscriber<DoubanLoginBean>(mView) {
                    @Override
                    public void onNext(DoubanLoginBean lastfm) {
                        ((DoubanContract.LoginView) mView).loginSucceed(lastfm);
                    }
                })
        );
    }

    @Override
    public void getChannels() {
        if (!(mView instanceof DoubanContract.ViewExt)) {
            throw new RuntimeException(mView.getClass().getName() + "must implements " + DoubanContract.LoginView.class.getName());
        }
        addSubscribe(mDataManager.getChannels()
                .compose(RxUtil.<DoubanChannelBean>rxSchedulerHelper())

                .subscribeWith(new CommonSubscriber<DoubanChannelBean>(mView) {
                    @Override
                    public void onNext(DoubanChannelBean channels) {
                        ((DoubanContract.ViewExt) mView).channelsLoaded(channels);
                    }
                })
        );
    }

//    @Override
//    public void getDefaultPlaylist() {
//
//    }

    @Override
    public void getPlaylist(String channelId) {
        if (!(mView instanceof DoubanContract.ViewExt)) {
            throw new RuntimeException(mView.getClass().getName() + "must implements " + DoubanContract.LoginView.class.getName());
        }
        String header = "Bearer 21a0867e86f16fb464e1937b94d44f1w";
        String from = "mainsite";
        String pt = "0.0";
        int kbps = 128;
        String formats = "aac";
        String alt = "json";
        String app_name = "radio_iphone";
        String apikey = Constants.DOUBAN_API_KEY;
        String client = "s:mobile|y:iOS 10.2|f:115|d:b88146214e19b8a8244c9bc0e2789da68955234d|e:iPhone7, 1|m:appstore";
        String client_id = Constants.DOUBAN_CLIENT_ID;
        String icon_cate = "xlarge";
        String udid = Constants.DOUBAN_UDID;
        String douban_udid = Constants.DOUBAN_DOUBAN_UDID;
        String version = "115";
        String type = "n";
        addSubscribe(mDataManager.getPlaylist(/*header, */channelId, from, pt, kbps, formats, alt, app_name, apikey, client, client_id, icon_cate, udid, douban_udid, version, type)
                .compose(RxUtil.<DoubanPlaylistBean>rxSchedulerHelper())

                .subscribeWith(new CommonSubscriber<DoubanPlaylistBean>(mView) {
                    @Override
                    public void onNext(DoubanPlaylistBean playlist) {
                        ((DoubanContract.ViewExt) mView).playlistLoaded(playlist);
                    }
                })
        );
    }

    @Override
    public void getLyric(int sid, String ssid) {
        if (!(mView instanceof DoubanContract.ViewExt)) {
            throw new RuntimeException(mView.getClass().getName() + "must implements " + DoubanContract.LoginView.class.getName());
        }
        addSubscribe(mDataManager.getLyric(sid, ssid)
                .compose(RxUtil.<DoubanLyricBean>rxSchedulerHelper())

                .subscribeWith(new CommonSubscriber<DoubanLyricBean>(mView) {
                    @Override
                    public void onNext(DoubanLyricBean lyric) {
                        ((DoubanContract.ViewExt) mView).lyricLoaded(lyric);
                    }
                })
        );
    }
}
